package per.chenxin.scale;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.List;

import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.htmlparser.util.ParserException;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

public class Main {

    private static Shell shell;

    private static Text textUrl;

    private static Browser browser;

    private static Proxy proxy = Proxy.getProxy();

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {

        Display display = Display.getDefault();
        shell = new Shell();
        Rectangle rectangle = display.getClientArea();
        shell.setSize((rectangle.width - rectangle.x) / 2, (rectangle.height - rectangle.y) / 2);
        shell.setLocation((rectangle.width - rectangle.x) / 4, (rectangle.height - rectangle.y) / 4);
        shell.setText("Scale Browser");
        shell.setLayout(new FillLayout(SWT.HORIZONTAL));

        Composite mainComposite = new Composite(shell, SWT.NONE);
        mainComposite.setLayout(new FillLayout(SWT.HORIZONTAL));

        SashForm sashForm = new SashForm(mainComposite, SWT.VERTICAL);

        Composite composite1 = new Composite(sashForm, SWT.BORDER);

        textUrl = new Text(composite1, SWT.BORDER);
        textUrl.setBounds(10, 10, 446, 18);

        Button btnGo = new Button(composite1, SWT.NONE);
        btnGo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                String strUrl = textUrl.getText();
                if (strUrl == null || strUrl.trim().length() == 0)
                    return;

                Socket socket = null;
                while (socket == null) {
                    try {
                        socket = HttpUtil.getHttpTunnel(HttpUtil.getHttpTunnel(
                            proxy.getExternalProxyHost(), proxy.getExternalProxyPort(), null, 0,
                            proxy.getInternalProxyHost(), proxy.getInternalProxyPort(),
                            proxy.getProxyAuthorization()), proxy.getExternalProxyHost(),
                            proxy.getExternalProxyPort(), null, 0);
                    } catch (IOException e1) {
                    }
                }

                try {
                    byte[] buffer = HttpUtil.getBytesFromInputStream(socket, strUrl);
                    String charset = HtmlUtil.getCharset(buffer);
                    String html = HtmlUtil.getHtmlFromBytes(buffer, charset);
                    List<String> links = HtmlUtil.getLinks(html, charset);
                    for (String link : links) {
                        System.out.println(link);
                    }
                    browser.setText(html);
                } catch (UnsupportedEncodingException e1) {
                    // TODO 自動生成された catch ブロック
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO 自動生成された catch ブロック
                    e1.printStackTrace();
                } catch (ParserException e1) {
                    // TODO 自動生成された catch ブロック
                    e1.printStackTrace();
                }

                //browser.setUrl(strUrl);
            }
        });
        btnGo.setBounds(462, 8, 61, 22);
        btnGo.setText("&Go");

        Button btnproxy = new Button(composite1, SWT.NONE);
        btnproxy.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {

                Proxy proxyTemp = (Proxy) new ProxyDialog(shell, SWT.DIALOG_TRIM, proxy).open();
                if (proxyTemp == null)
                    return;
                proxy = proxyTemp;
                try {
                    Proxy.export(proxy);
                } catch (FileNotFoundException e1) {
                } catch (IOException e1) {
                }
            }
        });
        btnproxy.setBounds(529, 8, 72, 22);
        btnproxy.setText("&Proxy");

        Composite composite2 = new Composite(sashForm, SWT.BORDER);
        composite2.setLayout(new FillLayout(SWT.HORIZONTAL));

        browser = new Browser(composite2, SWT.NONE);
        sashForm.setWeights(new int[] {
            44, 414
        });

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
